// import { useUserStore } from "@/store/modules/user";
// import { useLocaleStore } from "@/store/modules/locales";
// import { useTagsStore } from "@/store/modules/tags";

// import router from "@/router";
// const baseUrl = import.meta.env.VITE_APP_BASE_API;

import axios from "axios";
import { ElMessage } from "element-plus";
import { wrapperEnv } from "./common";

// const baseUrl = import.meta.env.VITE_APP_BASE_API;

// 创建axios对象
const requestHttp = axios.create({
  baseURL: wrapperEnv(),
  // baseURL: baseUrl,
  timeout: 30000,
});
// token的消息头
// const TOKEN_HEADER = "Authorization";
// const useUser = useUserStore();
// ================================= 请求拦截器 =================================
requestHttp.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// ================================= 响应拦截器 =================================

// 添加响应拦截器
requestHttp.interceptors.response.use(
  (response) => {
    // 对响应数据做点什么
    const res = response.data;

    if (res.code && res.code != 0) {
      if (res.code === 10001) {
        ElMessage({
          showClose: true,
          message: "登录过期，请重新登录！",
          type: "error",
          grouping: true,
        });

        // router.replace({ name: "login", query: {} });
      } else {
        ElMessage({
          showClose: true,
          message: res.message,
          type: "error",
          grouping: true,
        });
      }
      return Promise.reject(response);
    } else {
      if (res.data) {
        // 返回接口的data数据
        return Promise.resolve(res.data);
      } else {
        // 直接返回res，防止文件导出等报错
        return Promise.resolve(res);
      }
    }
  },
  (error) => {
    console.log("报错信息", error);
    if (error.message.indexOf("timeout") != -1) {
      ElMessage({
        showClose: true,
        message: "网络超时",
        type: "error",
        grouping: true,
      });
    } else if (error.message == "Network Error") {
      ElMessage({
        showClose: true,
        message: "网络连接错误",
        type: "error",
        grouping: true,
      });
    } else if (error.message.indexOf("Request") != -1) {
      ElMessage({
        showClose: true,
        message: "网络发生错误",
        type: "error",
        grouping: true,
      });
    }

    return Promise.reject(error);
  }
);

// ================================= 对外提供请求方法：通用请求，get， post, 下载download等 =================================

/**
 * 通用请求封装
 * @param config
 */
export const request = (config) => {
  return requestHttp.request(config);
};

/**
 * post请求
 */
export const postRequest = (url, data, responseType) => {
  const type = responseType || "json";
  return request({ data, url, method: "post", responseType: type });
};

/**
 * get请求
 */
export const getRequest = (url, params, responseType) => {
  const type = responseType || "json";
  return request({ url, method: "get", params, responseType: type });
};
/**
 * delete请求
 */
export const deleteRequest = (url, params) => {
  return request({ url, method: "delete", params });
};
